package com.ruoyi.framework.manager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ruoyi.common.utils.Threads;

import javax.annotation.PreDestroy;

/**
 * 
 * 热重启时部分资源(线程池、外部连接、退出死循环等)不一定能正常关闭,需要通过@PreDestroy手动关闭处理
 * 从Debug线程情况中可判断是否有关闭,若未关闭数量将一直增加
 * 确保应用退出时能关闭后台线程
 *
 * @author ruoyi
 */
@Component
public class ShutdownManager
{
    private static final Logger logger = LoggerFactory.getLogger("sys-user");

    @PreDestroy
    public void destroy()
    {
        shutdownAsyncManager();
        Threads.shutdownAndAwaitTermination(Threads.pool);
    }

    /**
     * 停止异步执行任务
     */
    private void shutdownAsyncManager()
    {
		try {
            logger.info("====系统退出/热重启,关闭后台资源(线程池、外部连接、退出死循环等)====");
            AsyncManager.me().shutdown();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
    }
}
